package com.pink.controller;

import com.auth0.jwt.interfaces.DecodedJWT;
import com.pink.entity.dto.UserDto;
import com.pink.entity.dto.VideoInfoDto;
import com.pink.entity.query.VideoInfoQuery;
import com.pink.entity.vo.ResponseVo;
import com.pink.service.UserService;
import com.pink.service.VideoInfoService;
import com.pink.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/admin/video")
@Validated
public class VideoInfoController {

    @Autowired
    VideoInfoService videoInfoService;

    @Resource
    UserService userService;

    @Resource
    JwtUtils utils;

    @RequestMapping("/addVideoInfo")
    public ResponseVo<String> addVideoInfo(HttpServletRequest request, VideoInfoDto videoInfo){
        String authHeader = request.getHeader("Authorization");
        DecodedJWT jwt = utils.resolveJwt(authHeader);
        Integer userId = utils.toId(jwt);
        //获取数据库中的用户信息
        UserDto User = userService.getUserByuserId(userId);
        return ResponseVo.success(videoInfoService.addVideoInfo(videoInfo,User));
    }

    // 修改视频信息
    @RequestMapping("/updateVideoInfo")
    public ResponseVo<String> updateVideoInfo(HttpServletRequest request, VideoInfoDto videoInfo){
        String authHeader = request.getHeader("Authorization");
        DecodedJWT jwt = utils.resolveJwt(authHeader);
        Integer userId = utils.toId(jwt);
        //获取数据库中的用户信息
        UserDto User = userService.getUserByuserId(userId);
        return ResponseVo.success(videoInfoService.updateVideoInfo(videoInfo,User));
    }

    // 删除视频信息
    @RequestMapping("/deleteVideoInfo")
    public ResponseVo<String> deleteVideoInfo(Integer videoId){
        return ResponseVo.success(videoInfoService.deleteVideoInfo(videoId));
    }

    // 加载数据列表
    @RequestMapping("/loadDataList")
    public ResponseVo<VideoInfoQuery> loadDataList(
            // 页码，默认为1
            @RequestParam(value = "PageNo", defaultValue = "1") int pageNo,
            // 每页大小，默认为15
            @RequestParam(value = "PageSize", defaultValue = "15") int pageSize,
            String title) {
        return ResponseVo.success(videoInfoService.findVideoInfoQueryList(pageNo, pageSize,title));
    }

}
